//
//  WBMainViewController.swift
//  MyXinLangWeiBo
//
//  Created by 迟浩伦 on 2016/12/22.
//  Copyright © 2016年 迟浩伦. All rights reserved.
//

import UIKit
//主控制器
class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
        setupComposeButton()
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    return .portrait
    }
    
    //MARK: - 监听方法
    func composeStatus(){

    }
    
    
    //MARK: - 私有控件
//懒加载中间控件
     lazy var composeButton: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
}
//MARK: - 设置界面
extension WBMainViewController{
    //设置中间按钮
     func setupComposeButton() {
        tabBar.addSubview(composeButton)
        let count = CGFloat((viewControllers?.count)!)
        let w = tabBar.bounds.width / count - 1//处理容错点
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }

     func setupChildControllers(){
        //0.获取沙盒json路径
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")

        //记载data
        var data = NSData(contentsOfFile: jsonPath)
        //判断data是否有内容，如果没有 说明本地沙盒没有文件
        if data == nil{
        //从Bundle加载data
            let path = Bundle.main.path(forResource: "main.json", ofType: nil)
            data = NSData(contentsOfFile: path!)
        }
        
        //data一定会有一个内容，反序列化
        
        
        //3.反序列化转换成数组
        guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String: Any]]
        else{
        return
        }
        //加载json  
        
        
    //很多应用程序 界面的创建都依赖网络的json
        //遍历数组 循环创建控制器组
       var arrayM = [UIViewController]()
        
        for dict in array! {
            arrayM.append(controller(dict: dict))//把控制器创建完添加到arrayM里
        }
        //设置taBbar的子控制器
        viewControllers = arrayM
    }
    //使用字典创建一个子控制器
    private func controller(dict: [String: Any]) -> UIViewController{
    // 取得字典内容
       guard let clsName = dict["clsName"] as? String,
        let title = dict["title"] as? String,
        let imageName = dict["imageName"] as? String,
        let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? WBBaseViewController.Type,
        let visitorDict = dict["visitorInfo"] as? [String: String]
        else{
        return UIViewController()
        }
        //创建视图控制器
        //将clsName转换成cls
        let vc = cls.init()
        
        vc.title = title
        //设置访客信息字典
        vc.visitorInfoDictionary = visitorDict
        
        //设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        //设置tabBar标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
        
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
    }

}
